package com.nbsystem.interceptor;


import com.nbsystem.entity.SysPer;
import com.nbsystem.entity.SysUser;
import com.nbsystem.exception.PrivilegesException;
import com.nbsystem.util.Const;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

public class SysPerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        String URL = httpServletRequest.getRequestURI();
        HttpSession session = httpServletRequest.getSession();
        SysUser sysUser = (SysUser) session.getAttribute(Const.USERSESSION);

        System.out.println(URL+"请求的路径");


        boolean flag = false;
        if (sysUser!=null){
            if (URL.contains("/index")){
                List<SysPer> sysPer = sysUser.getSysPer();
                for (SysPer sys :sysPer){
                    System.out.println("系统中的路径"+sys.getUrl());
                    if (URL.contains(sys.getUrl())){
                       flag = true;
                    }
                }
                if (!flag) {
                    throw  new PrivilegesException();
                }
            }
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
